from __future__ import with_statement
from es import getAddonPath
from cPickle import dump, load
from os.path import isfile, join

class dataManager(object):
    def __init__(self, database):
        self.path = join(getAddonPath('superdice'), 'data', database + '.db')
        self.data = {}
        if isfile(self.path):
            with open(self.path, 'rb') as file_object:
                self.data = load(file_object)

    def __iter__(self):
        for attr in self.data:
            yield attr

    def __list__(self):
        return self.data.keys()

    def __contains__(self, attr):
        return attr in self.data

    def __dict__(self):
        return self.data

    def __delitem__(self, attr):
        del self.data[attr]

    def __getitem__(self, attr):
        return self.data[attr]

    def __setitem__(self, attr, value):
        self.data[attr] = value

    def saveDatabase(self):
        with open(self.path, 'wb') as file_object:
            dump(self.data, file_object)
